angular.module('vote').controller('voteDetailCtrl', ['$scope', '$uibModalInstance', '$http', 'id', function ($scope, $uibModalInstance, $http, id) {
    $scope.vote = {
        title: '',
        description: '',
        questions: []
    };
    $scope.onInit = function () {
        if (id)
            $http.get(ctx + '/vote/detail/' + id).then(function (resp) {
                $scope.vote = resp.data;
            });
    }
    $scope.onInit();
    $scope.ok = function () {
        $http.post(ctx + '/vote/save', $scope.vote).then(function (resp) {
            $uibModalInstance.close();
        });
    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };

    $scope.addQuestion = function () {
        $scope.vote.questions.push({
            title: '',
            options: [],
            type: 'radio'
        });
    }
    $scope.removeQuestion = function (idx) {
        $scope.vote.questions.splice(idx, 1);
    }
    $scope.addOption = function (question) {
        question.options.push({
            title: '',
            isInput: "not"
        });
    }
    $scope.removeOption = function (question, idx) {
        question.options.splice(idx, 1);
    }
}]);